package com.wisdom.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bluecall.sipua.R;

public class ShowWebActivity extends Activity{

	private final static String TAG = "ShowWebActivity";
	protected String url = null;
	protected WebView mWebView = null;
	private ProgressBar mProgressBar = null;
	protected AsyncTask SendAccess = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_web_layout);
		url = getIntent().getAction();
		
		if (!TextUtils.isEmpty(url)) {
			mWebView = (WebView)findViewById(R.id.myblog_view);
			mProgressBar = (ProgressBar)findViewById(R.id.progress);
			initWebView();
			mWebView.loadUrl(url);
		}
	}

	protected void initWebView() {
		// 初始化网页设置
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setCacheMode(WebSettings.LOAD_NORMAL);			  // 正常加载缓存
		webSettings.setJavaScriptEnabled(true);						 // 支持JavaScript
		webSettings.setSavePassword(false);							 // 不保存密码
		webSettings.setSupportZoom(true);							  // 不支持网页缩放
		webSettings.setBuiltInZoomControls(true);
		webSettings.setPluginsEnabled(true);
		mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);  // 滚动条悬浮在网页上，不占页面宽度
		mWebView.setOnCreateContextMenuListener(this);
		mWebView.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				mProgressBar.setVisibility(View.VISIBLE);
				super.onPageStarted(view, url, favicon);
			}
			public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
				 handler.proceed() ;
			 }
			
			@Override
			public void onPageFinished(WebView view, String url) {
				mProgressBar.setVisibility(View.GONE);
				super.onPageFinished(view, url);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
			    if (!url.equals("about:blank")) {
	                mWebView.loadUrl(url);
			    }
				return false;
			}
		});
		mWebView.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				mProgressBar.setProgress(newProgress);
				super.onProgressChanged(view, newProgress);
			}

			@Override
			public boolean onJsAlert(WebView view, String url, String message,
					final JsResult result) {
				AlertDialog.Builder b = new AlertDialog.Builder(ShowWebActivity.this);
				b.setTitle(R.string.app_name);
				b.setMessage(message);
				b.setPositiveButton(android.R.string.ok,
						new AlertDialog.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								result.confirm();
							}
						});
				b.setCancelable(false);
				b.create();
				b.show();
				return true;
			};

			@Override
			public boolean onJsConfirm(WebView view, String url, String message,
					final JsResult result) {
				AlertDialog.Builder b = new AlertDialog.Builder(ShowWebActivity.this);
				b.setTitle(R.string.app_name);
				b.setMessage(message);
				b.setPositiveButton(android.R.string.ok,
						new AlertDialog.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								result.confirm();
							}
						});
				b.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								result.cancel();
							}
						});
				b.setCancelable(false);
				b.create();
				b.show();
				return true;
			};

			@Override
			public boolean onJsPrompt(WebView view, String url, String message,
					String defaultValue, final JsPromptResult result) {

				return true;
			};
		});
	}
	

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		if (mWebView.canGoBack()) {
			menu.getItem(0).setEnabled(true);
		} else {
			menu.getItem(0).setEnabled(false);
		}
		if (mWebView.canGoForward()) {
			menu.getItem(1).setEnabled(true);
		} else {
			menu.getItem(1).setEnabled(false);
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		WebView.enablePlatformNotifications();
	}

	@Override
	protected void onPause() {
		super.onPause();
		WebView.disablePlatformNotifications();
	}

	@Override
	protected void onDestroy() {
		mWebView.stopLoading();
		mWebView.destroy();
		mWebView = null;
		super.onDestroy();
	}

}
